import requests
import parsel
import json
from redis import StrictRedis


def main():
    url = 'https://s.weibo.com/top/summary?cate=realtimehot'
    # 搜索网址
    cookie = 'SUB=_2AkMWwF4Bf8NxqwJRmfoXyGvga4h3zgDEieKgnK_aJRMxHRl-yT8XqkA-tRB6PUBw7coYNnak48M0z3qtiVhf6c4zcsIQ; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWd.09Zg3BPQpGYxJmopaki; SINAGLOBAL=3965563815155.926.1637667128403; ULV=1639571885320:2:1:1:3746263292384.1543.1639571885268:1637667128808; _s_tentry=-; Apache=3746263292384.1543.1639571885268'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        'Cookie' : cookie
    }  # 伪装
    # 伪装爬虫
    r = requests.get(url, headers=headers)  # 请求网站
    body = r.content.decode('utf-8',"ignore")
    sel = parsel.Selector(body)
    name = sel.css(".td-02>a::text").getall()
    link = sel.css(".td-02>a::attr(href)").getall()
    number = sel.css(".td-02>span::text").getall()
    name_list = []
    link_list = []
    number_list = []
    data_list = []
    for i in range(1,len(name)):
        name_list.append(name[i])
    for i in range(1,len(link)):
        if(link[i] == "javascript:void(0);"):
            continue
        link_list.append( "https://s.weibo.com" + link[i])
    for i in number:
        number_list.append(i)
    for i in range(0, 11):
        if(i==3):
            continue
        data = {}
        data['name'] = name_list[i]
        data['number'] = number_list[i]
        data['link'] = link_list[i]
        data_list.append(data)
    redis = StrictRedis(host='localhost', port=6379, decode_responses=True)
    save_redis(redis, data_list)


def save_redis(redis, data_list):
    redis.delete('weibo_hot')
    for o in data_list:
        # s = json.dumps(o.__dict__, ensure_ascii=False)
        s = json.dumps(o, ensure_ascii=False)
        print(s)
        redis.rpush('weibo_hot', s)


if __name__ == '__main__':
    main()

